using Items;
using Services;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class EquipmentUI : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    public static UnityAction<Equipment> RightClickEquipment;

    [SerializeField]
    private EEqiupPart part;
    public EEqiupPart Part => part;
    [SerializeField]
    private ESect sect;
    public ESect Sect => sect;

    private Player player;

    private Services.EventSystem eventSystem;
    private Text m_text;
    private Image m_image;
    private Equipment m_equipment;
    private float offset;

    private Sprite icon_default;

    private void Awake()
    {
        RectTransform rectTransform = GetComponent<RectTransform>();
        offset = rectTransform.rect.width / 1600 * Screen.width;
        m_image = GetComponent<Image>();
        icon_default = m_image.sprite;
        m_text = GetComponentInChildren<Text>();
        eventSystem = ServiceLocator.Get<Services.EventSystem>();
    }

    private void Start()
    {
        player = ServiceLocator.Get<ActionController>().player;
    }

    public void Show(Equipment equipment)
    {
        m_equipment = equipment;
        if (equipment == null)
        {
            m_text.text = null;
            m_image.sprite = icon_default;
        }
        else
        {
            m_text.text = m_equipment.name;
            m_image.sprite = equipment.icon;
        }
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        if (m_equipment != null)
        {
            string infomation = m_equipment.GetInformation(player);
            eventSystem.Invoke<string, Vector2, float>(EEvent.View, infomation, transform.position, offset);
        }
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        eventSystem.Invoke(EEvent.EndView);
    }
}